// pages/my/index.js
import {
    staff, wechat
  } from "../../api/apiData"
  import Toast from '@vant/weapp/toast/toast';
Page({

    /**
     * 页面的初始数据
     */
    data: {
        type:"",
        myinfo:'',
        is_shrink:false
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        let type = wx.getStorageSync('type')
        console.log(type)
        this.setData({
            type
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        let type = wx.getStorageSync('type')
        console.log(type)
        this.setData({
            type
        })
        this.getTabBar().init();
        if(this.data.type == "Staff"){
            this.GetMyInfo()
        }
        if(this.data.type == "" || this.data.type == undefined){
            wx.showModal({
                title: '提示',
                content: '您未登录,请登录~',
                showCancel: true,//是否显示取消按钮
                cancelText:"否",//默认是“取消”
                // cancelColor:'skyblue',//取消文字的颜色
                confirmText:"是",//默认是“确定”
                // confirmColor: 'skyblue',//确定文字的颜色
                success: function (res) {
                   if (res.cancel) {
                      //点击取消,默认隐藏弹框
                      wx.navigateBack({
                        delta: 1,
                      })
                   } else {
                      //点击确定
                     wx.navigateTo({
                       url: "/pages/login/index",
                     })
                   }
                },
                fail: function (res) { },//接口调用失败的回调函数
                complete: function (res) { },//接口调用结束的回调函数（调用成功、失败都会执行）
             })
        }
        
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    },
    GetMyInfo(){
        let _this = this
        Toast.loading()
        staff.info({},res=>{
            console.log(res)
            let {code,msg,data} = res
            if(code == 1) {
                _this.setData({
                    myinfo:data
                })
                Toast.clear()
            }else{
                Toast(msg)
            }
        })
    },
    ExitLoding(){
        wx.setStorage({key:"token",data:""})
        wx.setStorage({key:"type",data:""})
        wx.navigateTo({
          url: '/pages/login/index',
        })
    },
    setshrink(){
        this.setData({
            is_shrink:!this.data.is_shrink
        })
    },
    bindWx(){
        let openid = this.data.myinfo.openid
        let myinfo = this.data.myinfo
        let _this = this
        if(openid == ""){
            wx.login({
                success(res){
                  console.log(res)
                  let {code} = res
                  staff.bind({code},res=>{
                      Toast(res.msg)
                      if(res.code == 1){
                          myinfo.openid = "1"
                        _this.setData({
                            myinfo
                        })
                      }
                  })
                }
              })
        }else{
            staff.unbind({},res=>{
                Toast(res.msg)
                if(res.code==1){
                    myinfo.openid = ""
                        _this.setData({
                            myinfo
                        })
                }

                 })
        }

        
    },

    
})